const mysql = require('mysql2/promise');

// 数据库配置
const dbConfig = {
  host: 'localhost',
  user: 'root',
  password: '123456',
  database: 'test2_nextjs',
  charset: 'utf8mb4'
};

// 文档中提到的ID列表
const expectedIds = [
  2, 301, 487, 533,  // 许昌子区域
  285, 286, 333, 337, // 洛阳子区域
  303, 311, 414,      // 长安子区域
  93, 112, 113,       // 成都子区域
  52, 143, 351,       // 南中子区域
  306, 307, 308,      // 汉中子区域
  267,                 // 建业子区域
  220, 221, 222,      // 长沙子区域
  137, 161, 163       // 江陵子区域
];

async function checkSpecificIds () {
  const connection = await mysql.createConnection(dbConfig);

  try {
    console.log('🔍 检查文档中提到的特定ID...\n');

    console.log('📋 期望的ID列表:');
    expectedIds.forEach(id => console.log(`  ${id}`));
    console.log('');

    // 检查每个ID是否存在
    let foundIds = [];
    let missingIds = [];

    for (const id of expectedIds) {
      try {
        const [rows] = await connection.execute(
          'SELECT id, name, parent_id, extension FROM map_areas WHERE id = ?',
          [id]
        );

        if (rows.length > 0) {
          const area = rows[0];
          foundIds.push(area);
          console.log(`✅ ID ${id}: ${area.name} (父级: ${area.parent_id || 'NULL'}, 扩展: ${area.extension || 'NULL'})`);
        } else {
          missingIds.push(id);
          console.log(`❌ ID ${id}: 不存在`);
        }
      } catch (error) {
        console.error(`❌ 查询ID ${id} 失败:`, error.message);
        missingIds.push(id);
      }
    }

    console.log('\n📊 检查结果统计:');
    console.log(`  找到的ID: ${foundIds.length} 个`);
    console.log(`  缺失的ID: ${missingIds.length} 个`);
    console.log(`  总计检查: ${expectedIds.length} 个`);

    if (missingIds.length > 0) {
      console.log('\n❌ 缺失的ID:');
      missingIds.forEach(id => console.log(`  ${id}`));
    }

    if (foundIds.length > 0) {
      console.log('\n✅ 找到的区域:');
      foundIds.forEach(area => {
        const parentInfo = area.parent_id ? `(父级: ${area.parent_id})` : '(顶级)';
        const extensionInfo = area.extension ? ` ${area.extension}` : '';
        console.log(`  ID: ${area.id} | ${area.name}${extensionInfo} | ${parentInfo}`);
      });
    }

    // 检查是否有其他区域可以作为子区域
    console.log('\n🔍 检查现有的子区域关系...');
    const [subAreas] = await connection.execute(
      'SELECT id, name, parent_id, extension FROM map_areas WHERE parent_id IS NOT NULL ORDER BY parent_id, id'
    );

    if (subAreas.length > 0) {
      console.log('📋 现有的子区域:');
      subAreas.forEach(area => {
        const extensionInfo = area.extension ? ` ${area.extension}` : '';
        console.log(`  ID: ${area.id} | ${area.name}${extensionInfo} | 父级: ${area.parent_id}`);
      });
    } else {
      console.log('⚠️  没有找到任何子区域');
    }

  } catch (error) {
    console.error('❌ 检查失败:', error);
  } finally {
    await connection.end();
  }
}

// 运行脚本
if (require.main === module) {
  checkSpecificIds().catch(console.error);
}

module.exports = { checkSpecificIds }; 